package com.furns.service.impl;

import com.furns.dao.FurnDAO;
import com.furns.dao.OrderDAO;
import com.furns.dao.OrderItemDAO;
import com.furns.dao.impl.FurnDAOImpl;
import com.furns.dao.impl.OrderDAOImpl;
import com.furns.dao.impl.OrderItemDAOImpl;
import com.furns.entity.*;
import com.furns.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
public class OrderServiceImpl implements OrderService {
    private OrderDAO orderDAO =new OrderDAOImpl() ;
    private OrderItemDAO orderItemDAO = new OrderItemDAOImpl();
    private FurnDAO furnDAO = new FurnDAOImpl();


    @Override
    public String saveOrder(Cart cart, int MemberId) {
        //1.生成订单
        String  orderId=System.currentTimeMillis()+""+ MemberId;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Order order = new Order(orderId, sdf.format(new Date()), cart.getCartTotalPrice(), 0, MemberId);
        orderDAO.saveOrder(order);
        //2.生成订单项
        HashMap<Integer, CartItem> items = cart.getItems();
        for (Integer id : items.keySet()) {
            CartItem item = items.get(id);
            OrderItem orderItem = new OrderItem(null,item.getName(),item.getPrice(),item.getCount(),item.getTotalPrice(),orderId);
            orderItemDAO.saveOrderItem(orderItem);
            //更新家居的销量存量
            Furn furn = furnDAO.queryFurnById(id);
            furn.setSales(furn.getSales()+item.getCount());
            furn.setStock(furn.getStock()-item.getCount());
            //更新数据库家居
            furnDAO.updateFurn(furn);
        }
        cart.clear();
        return orderId;
    }

    @Override
    public Order showOrder(String orderId) {
        return orderDAO.showOrder(orderId);
    }

    @Override
    public List<OrderItem> showOrderItem(String orderId) {
        return orderItemDAO.showOrderItem(orderId);
    }

    @Override
    public Page<Order> page(int pageNo, int pageSize) {
        //创建一个page对象,填充属性
        Page<Order> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = orderDAO.getTotalRow();
        page.setTotalRow(totalRow);
        int pageTotalCount = totalRow/pageSize;
        if(totalRow%pageSize>0){
            pageTotalCount+=1;
        }
        page.setPageTotalCount(pageTotalCount);
        int begin = (pageNo-1)*pageSize;
        List<Order> pageItems = orderDAO.getPageItems(begin, pageSize);

        page.setItems(pageItems);

        return page;
    }

    @Override
    public Page<Order> page(int memberId, int pageNo, int pageSize) {
        //创建一个page对象,填充属性
        Page<Order> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = orderDAO.getTotalRow(memberId);
        page.setTotalRow(totalRow);
        int pageTotalCount = totalRow/pageSize;
        if(totalRow%pageSize>0){
            pageTotalCount+=1;
        }
        page.setPageTotalCount(pageTotalCount);
        int begin = (pageNo-1)*pageSize;
        List<Order> pageItems = orderDAO.getPageItems(memberId,begin, pageSize);

        page.setItems(pageItems);

        return page;
    }
}
